package com.song.controller.admin;

import com.alibaba.fastjson.JSON;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.song.pojo.Employee;
import com.song.pojo.Menu;
import com.song.service.MenuService;
import com.song.utils.*;
import com.song.vo.MenuVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpSession;
import java.util.*;

/**
 * @author 宋超
 * HP - the login name of the current user.
 * 2020/12/6 - the current system date.
 * 9:55 - the current system time.
 */

@RestController
@RequestMapping("/admin/menu")
public class MenuController {

    @Autowired
    private MenuService menuService;

    /**
     * 加载左边菜单选项
     * @return
     */
    @RequestMapping("/loadMenuList")
    public String loadMenuList(HttpSession session){
        //查询所有菜单列表的方法
        //List<Menu> menuList = menuService.findMenuList();

        //通过session获取登录用户信息
        Employee employee = (Employee) session.getAttribute(SystemConstant.LOGINUSER);

        //根据当前登录用户的角色id 动态显示菜单列表
        List<Menu> menuList = menuService.findListByEmployeeId(employee.getId());

        //创建Map集合，保存MenuInfo菜单信息
        Map<String,Object> map = new LinkedHashMap<String,Object>();
        //创建Map集合，保存homeInfo信息
        Map<String,Object> homeInfo = new LinkedHashMap<String,Object>();
        //创建Map集合，保存logoInfo信息
        Map<String,Object> logoInfo = new LinkedHashMap<String,Object>();

        List<MenuNode> menuNodes = new ArrayList<MenuNode>();
        //循环菜单列表，显示菜单层级
        for (Menu menu : menuList) {
            //创建菜单节点对象
            MenuNode menuNode = new MenuNode();
            menuNode.setId(menu.getId());//菜单编号
            menuNode.setPid(menu.getPid());//父级菜单编号
            menuNode.setSpread(menu.getSpread());//是否展开
            menuNode.setTarget(menu.getTarget());//打开方式
            menuNode.setTitle(menu.getTitle());//菜单名称
            menuNode.setIcon(menu.getIcon());//菜单图标
            menuNode.setHref(menu.getHref());//链接地址
            //添加到list集合当中
            menuNodes.add(menuNode);
        }
        //保存HomeInfo
        homeInfo.put("title","首页");
        homeInfo.put("href","/admin/welcome.html");
        //保存logoInfo
        logoInfo.put("title","酒店管理系统");
        logoInfo.put("image","/statics/layui/images/logo.png");
        logoInfo.put("href","/admin/home.html");
        //将menuInfo，homeInfo，logoInfo添加到map中
        map.put("menuInfo", TreeUtil.toTree(menuNodes,0));
        map.put("homeInfo",homeInfo);
        map.put("logoInfo",logoInfo);
        return JSON.toJSONString(map);
    }


    /**
     * 加载菜单管理页面的左侧导航树
     * @return
     */
    @RequestMapping("/loadMenuTree")
    public DataGridViewResult loadMenuTree(){
        //调用查询所有菜单列表的方法
        List<Menu> menuList = menuService.findMenuList();
        //创建集合保存节点信息
        List<TreeNode> treeNodes = new ArrayList<TreeNode>();
        //循环遍历菜单列表集合
        for (Menu menu : menuList) {
            //判断当前菜单是否展开
            Boolean spread = (menu.getSpread()==null || menu.getSpread()==1) ? true : false;
            //将菜单信息保存到treeNodes集合中
            treeNodes.add(new TreeNode(menu.getId(),menu.getPid(),menu.getTitle(),spread));
        }
        //返回数据
        return new DataGridViewResult(treeNodes);
    }

    //分页查询菜单列表
    @RequestMapping("/list")
    public DataGridViewResult list(MenuVo menuVo){
        //设置分页信息
        PageHelper.startPage(menuVo.getPage(),menuVo.getLimit());
        //查询菜单列表
        List<Menu> menuList = menuService.findMenuListByPage(menuVo);
        //创建分页对象
        PageInfo<Menu> pageInfo = new PageInfo<Menu>(menuList);
        //返回数据
        return new DataGridViewResult(pageInfo.getTotal(),pageInfo.getList());
    }


    //添加菜单
    @RequestMapping("/addMenu")
    public String addMenu(Menu menu){
        Map<String,Object> map = new HashMap<String,Object>();
        //调用新增菜单的方法
        if(menuService.addMenu(menu)>0){
            map.put(SystemConstant.SUCCESS,true);
            map.put(SystemConstant.MESSAGE,"添加成功");
        }else{
            map.put(SystemConstant.SUCCESS,false);
            map.put(SystemConstant.MESSAGE,"添加失败");
        }
        return JSON.toJSONString(map);
    }

    //修改菜单
    @RequestMapping("/updateMenu")
    public String updateMenu(Menu menu){
        Map<String,Object> map = new HashMap<String,Object>();
        //调用新增菜单的方法
        if(menuService.updateMenu(menu)>0){
            map.put(SystemConstant.SUCCESS,true);
            map.put(SystemConstant.MESSAGE,"修改成功");
        }else{
            map.put(SystemConstant.SUCCESS,false);
            map.put(SystemConstant.MESSAGE,"修改失败");
        }
        return JSON.toJSONString(map);
    }

    //通过id查询此菜单下是否有子菜单
    @RequestMapping("/checkMenuHasChild")
    public String checkMenuHasChild(Integer id){
        Map<String,Object> map = new HashMap<String,Object>();
        //调用新增菜单的方法
        if(menuService. getMenuCountByMenuId(id)>0){
            map.put(SystemConstant.EXISI,true);
            map.put(SystemConstant.MESSAGE,"该菜单下存在子菜单,无法删除");
        }else{
            map.put(SystemConstant.EXISI,false);
        }
        return JSON.toJSONString(map);
    }

    //删除菜单
    @RequestMapping("/deleteById")
    public String deleteById(Integer id){
        Map<String,Object> map = new HashMap<String,Object>();
        //调用新增菜单的方法
        if(menuService.deleteById(id)>0){
            map.put(SystemConstant.SUCCESS,true);
            map.put(SystemConstant.MESSAGE,"删除成功");
        }else{
            map.put(SystemConstant.SUCCESS,false);
            map.put(SystemConstant.MESSAGE,"删除失败");
        }
        return JSON.toJSONString(map);
    }

}
